/*
*  Copyright 2019-2023 CY
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.ejweixin.service.impl;

import me.zhengjie.modules.ejweixin.domain.NgConstructionSiteInfo;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import me.zhengjie.modules.ejweixin.service.NgConstructionSiteInfoService;
import me.zhengjie.modules.ejweixin.domain.vo.NgConstructionSiteInfoQueryCriteria;
import me.zhengjie.modules.ejweixin.mapper.NgConstructionSiteInfoMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author cy
* @date 2025-04-22
**/
@Service
@RequiredArgsConstructor
public class NgConstructionSiteInfoServiceImpl extends ServiceImpl<NgConstructionSiteInfoMapper, NgConstructionSiteInfo> implements NgConstructionSiteInfoService {

    private final NgConstructionSiteInfoMapper ngConstructionSiteInfoMapper;

    @Override
    public PageResult<NgConstructionSiteInfo> queryAll(NgConstructionSiteInfoQueryCriteria criteria, Page<Object> page){
        return PageUtil.toPage(ngConstructionSiteInfoMapper.findAll(criteria, page));
    }

    @Override
    public List<NgConstructionSiteInfo> queryAll(NgConstructionSiteInfoQueryCriteria criteria){
        return ngConstructionSiteInfoMapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(NgConstructionSiteInfo resources) {
        save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(NgConstructionSiteInfo resources) {
        NgConstructionSiteInfo ngConstructionSiteInfo = getById(resources.getId());
        ngConstructionSiteInfo.copy(resources);
        saveOrUpdate(ngConstructionSiteInfo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<Integer> ids) {
        removeBatchByIds(ids);
    }

    @Override
    public void download(List<NgConstructionSiteInfo> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (NgConstructionSiteInfo ngConstructionSiteInfo : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("所属团队ID", ngConstructionSiteInfo.getTeacherId());
            map.put("工地示例图", ngConstructionSiteInfo.getTittleImgPath());
            map.put("工地位置", ngConstructionSiteInfo.getConstructionAddress());
            map.put("施工类型", ngConstructionSiteInfo.getConstructionType());
            map.put("房间类型", ngConstructionSiteInfo.getRoomType());
            map.put("户型", ngConstructionSiteInfo.getHouseType());
            map.put("设计风格", ngConstructionSiteInfo.getDesignStyle());
            map.put("所属区县", ngConstructionSiteInfo.getDistrict());
            map.put("浏览次数", ngConstructionSiteInfo.getNumberViews());
            map.put("方式  半包全包", ngConstructionSiteInfo.getWay());
            map.put("预算", ngConstructionSiteInfo.getBudget());
            map.put("进度共计16个阶段", ngConstructionSiteInfo.getProgress());
            map.put("创建者", ngConstructionSiteInfo.getCreateBy());
            map.put("更新者", ngConstructionSiteInfo.getUpdateBy());
            map.put("创建日期", ngConstructionSiteInfo.getCreateTime());
            map.put("更新时间", ngConstructionSiteInfo.getUpdateTime());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public NgConstructionSiteInfo getConstructionSiteDetailsById(String id, Long userId) {
        NgConstructionSiteInfo data = ngConstructionSiteInfoMapper.findById(id, userId);
        int count = data.getNumberViews();
        count ++;
        ngConstructionSiteInfoMapper.updateViewCount(count, data.getId());
        return data;
    }

    @Override
    public List<NgConstructionSiteInfo> getConstructionSiteByStaffId(Long id) {
        return ngConstructionSiteInfoMapper.getConstructionSiteByStaffId(id);
    }

    @Override
    public void updateStaffViewCount(Integer siteId, Long userId) {
        ngConstructionSiteInfoMapper.updateStaffViewCount(siteId, userId);
    }
}